package com.yupi.springbootinit.manager;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.datasource.*;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.exception.ThrowUtils;
import com.yupi.springbootinit.model.dto.post.PostQueryRequest;
import com.yupi.springbootinit.model.dto.search.SearchRequest;
import com.yupi.springbootinit.model.dto.user.UserQueryRequest;
import com.yupi.springbootinit.model.entity.Picture;
import com.yupi.springbootinit.model.enums.SearchTypeEnum;
import com.yupi.springbootinit.model.vo.PostVO;
import com.yupi.springbootinit.model.vo.SearchVO;
import com.yupi.springbootinit.model.vo.UserVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.CompletableFuture;

/**
 * 搜索门面
 */
@Component
public class SearchFacade {

    @Resource
    private DataSourceRegistry dataSourceRegistry;

    public SearchVO searchAll(@RequestBody SearchRequest searchRequest, HttpServletRequest request){
        String type = searchRequest.getType();
        SearchTypeEnum searchTypeEnum = SearchTypeEnum.getEnumByValue(type);
        ThrowUtils.throwIf(StringUtils.isBlank(type), ErrorCode.PARAMS_ERROR);
        String searchText = searchRequest.getSearchText();

        Page<UserVO> userVOPage = null;
        Page<PostVO> postPage = null;
        Page<Picture> picturePage = null;
        //搜索出所有的异常
        long current = searchRequest.getCurrent();
        long pageSize = searchRequest.getPageSize();
        if(searchRequest==null){
            //使用并发
            CompletableFuture<Page<UserVO>> userTask = CompletableFuture.supplyAsync(() -> {
                Page<UserVO> userVOPageTask = dataSourceRegistry.getDataSourceByType("user").doSearch(searchText, current, pageSize);
                return userVOPageTask;
            });
            CompletableFuture<Page<PostVO>> postTask = CompletableFuture.supplyAsync(() -> {
                Page<PostVO> postPageTask = dataSourceRegistry.getDataSourceByType("post").doSearch(searchText, current, pageSize);
                return postPageTask;
            });
            CompletableFuture<Page<Picture>> pictureTask = CompletableFuture.supplyAsync(() -> {
                Page<Picture> picturePageTask = dataSourceRegistry.getDataSourceByType("picture").doSearch(searchText, pageSize, current);
//                Page<Picture> picturePageTask = pictureService.searchPicture(searchText, pageSize,current);
                return picturePageTask;
            });
            //相当于是一个阻塞，只要前三个线程都执行完之后，才会向下执行
            CompletableFuture.allOf(userTask,postTask,pictureTask).join();

            try {
                userVOPage = userTask.get();
                postPage = postTask.get();
                picturePage = pictureTask.get();
            } catch (Exception e) {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR,"获取参数异常");
            }
            SearchVO searchVO = new SearchVO();
            searchVO.setPictureList(picturePage.getRecords());
            searchVO.setPostList(postPage.getRecords());
            searchVO.setUserList(userVOPage.getRecords());
            return searchVO;
        }else {

            DataSource dataSource1 = dataSourceRegistry.getDataSourceByType(type);
            Page<?> page1 = dataSource1.doSearch(searchText, current, pageSize);
            //使用并发
            SearchVO searchVO = new SearchVO();
            searchVO.setDataList(page1.getRecords());

            return searchVO;
        }



    }
}
